package com.hellobike.haxing.modules.wms.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.common.system.query.QueryGenerator;
import com.hellobike.haxing.common.util.oConvertUtils;
import com.hellobike.haxing.modules.shop.entity.Shop;
import com.hellobike.haxing.modules.shop.mapper.ShopMapper;
import com.hellobike.haxing.modules.wms.dto.WarehouseShopDto;
import com.hellobike.haxing.modules.wms.entity.Warehouse;
import com.hellobike.haxing.modules.wms.entity.WarehouseShop;
import com.hellobike.haxing.modules.wms.service.IWarehouseService;
import com.hellobike.haxing.modules.wms.service.IWarehouseShopService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import com.hellobike.haxing.common.system.base.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.hellobike.haxing.common.annotation.AutoLog;

/**
 * @Description: t_wms_warehouse_shop
 * @Author: haxing-boot
 * @Date: 2020-02-07
 * @Version: V1.0
 */
@RestController
@RequestMapping("/wms/warehouseShop")
@Slf4j
public class WarehouseShopController extends WmsBaseController<WarehouseShop, IWarehouseShopService> {
    @Autowired
    private IWarehouseShopService warehouseShopService;
    @Autowired
    private IWarehouseService warehouseService;

    @Autowired
    private ShopMapper shopMapper;

    /**
     * 分页列表查询
     *
     * @param warehouseShop
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "t_wms_warehouse_shop-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(WarehouseShopDto warehouseShop,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        Page<WarehouseShopDto> page = new Page<>(pageNo, pageSize);
        IPage<WarehouseShopDto> pageList = warehouseShopService.pageByWarehouseShopDto(page, warehouseShop);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param warehouseShop
     * @return
     */
    @AutoLog(value = "t_wms_warehouse_shop-添加", operateType = 2)
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody WarehouseShop warehouseShop) {
        warehouseShopService.save(warehouseShop);
        updateShop(warehouseShop.getShopId());
        return Result.ok("添加成功！");
    }

    private void updateShop(Long shopId) {
        Shop param = new Shop();
        param.setId(shopId);
        param.setUpdateTime(new Date());
        shopMapper.updateById(param);
    }

    /**
     * 编辑
     *
     * @param warehouseShop
     * @return
     */
    @AutoLog(value = "t_wms_warehouse_shop-编辑", operateType = 3)
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody WarehouseShopDto warehouseShop) {
        WarehouseShop shop = warehouseShopService.getByShopId(warehouseShop.getShopId());
        if (null == shop) {
            shop = new WarehouseShop();
            shop.setWarehouseId(warehouseShop.getWarehouseId());
            shop.setShopId(warehouseShop.getShopId());
            shop.setActiveStatus("OPEN");
            warehouseShopService.save(shop);
        } else {
            if (!shop.getWarehouseId().equals(warehouseShop.getWarehouseId())) {
                shop.setWarehouseId(warehouseShop.getWarehouseId());
                warehouseShopService.updateById(shop);
            }
        }
		updateShop(warehouseShop.getShopId());
//		warehouseShopService.updateById(warehouseShop);
        return Result.ok("编辑成功!");
    }

    @GetMapping(value = "/getWarehouse")
    public Result<?> getWarehouse() {
        List<Warehouse> warehouses = warehouseService.getBaseMapper().selectList(new QueryWrapper<>());
        return Result.ok(warehouses);
    }


    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "t_wms_warehouse_shop-删除", operateType = 4)
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        warehouseShopService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "t_wms_warehouse_shop-批量删除", operateType = 4)
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.warehouseShopService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        WarehouseShop warehouseShop = warehouseShopService.getById(id);
        if (warehouseShop == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(warehouseShop);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param warehouseShop
     */
    @AutoLog(value = "t_wms_warehouse_shop-导出excel", operateType = 6)
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, WarehouseShop warehouseShop) {
        return super.exportXls(request, warehouseShop, WarehouseShop.class, "t_wms_warehouse_shop");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @AutoLog(value = "t_wms_warehouse_shop-导入excel", operateType = 5)
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, WarehouseShop.class);
    }

}
